package com.a0soft.gphone.aTruffleHog.wnd.stat;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import java.text.DecimalFormat;

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.gio.ui.BaseActivity;
import com.a0soft.gphone.aTruffleHog.pref.Pref;
import com.a0soft.gphone.aTruffleHog.util.aDebug;
import com.a0soft.gphone.aTruffleHog.widget.RangeSeekBar;
import com.a0soft.gphone.aTruffleHog.widget.RangeSeekBar.OnRangeSeekBarChangeListener;
import com.a0soft.gphone.aTruffleHog.wnd.BaseFragment;
import com.a0soft.gphone.aTruffleHog.wnd.info.InfoViewCONSTS;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FindDevicesByCameraFormFrg extends BaseFragment {
    public static final int CAMERA_PIXELS_IDX_MIN = 0; //100K
    public static final int CAMERA_PIXELS_IDX_MAX = 20; //20M
    
    private static final String EXTRA_PREFIX = FindDevicesByCameraFormFrg.class.getName();
    public static final String EXTRA_CAMERA_PIXELS_MIN = EXTRA_PREFIX + ".min";
    public static final String EXTRA_CAMERA_PIXELS_MAX = EXTRA_PREFIX + ".max";
    
    private TextView m_tvMin, m_tvMax;
    private RangeSeekBar<Integer> m_sb;
    
    private static final String LOG_TAG = FindDevicesByCameraFormFrg.class.getSimpleName();
    
    ////////////////////////////////////////
    public static FindDevicesByCameraFormFrg 
    NewInstance() 
    {
        FindDevicesByCameraFormFrg frg = new FindDevicesByCameraFormFrg();
        return frg;
    }
    
    @Override public View 
    onCreateView(LayoutInflater inflater, ViewGroup vgContainer,
                 Bundle bdSavedInstanceState) 
    {
        if (aDebug.ENABLED)
            aDebug.d(LOG_TAG, "onCreateView()");
        
        Context ctx = getActivity();
        
        View vRoot = inflater.inflate(R.layout.find_device_by_camera_form_frg, vgContainer, false);
        
        m_tvMin = (TextView)vRoot.findViewById(R.id.min);
        m_tvMax = (TextView)vRoot.findViewById(R.id.max);
        
        RangeSeekBar<Integer> seekBar = new RangeSeekBar<Integer>(
                CAMERA_PIXELS_IDX_MIN, CAMERA_PIXELS_IDX_MAX, 
                ctx);
        m_sb = seekBar;
        seekBar.setNotifyWhileDragging(true);
        seekBar.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>() {
            @Override public void 
            onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, 
                                        Integer minValue, Integer maxValue)
            {
                _UpdateSeekBar();
            }
        });
        
        seekBar.setSelectedMaxValue(Pref.GetMaxCameraPixelsIdx(ctx));
        seekBar.setSelectedMinValue(Pref.GetMinCameraPixelsIdx(ctx));
        
        ViewGroup layout = (ViewGroup)vRoot.findViewById(R.id.container);
        layout.addView(seekBar);
        
        vRoot.findViewById(R.id.find).setOnClickListener(new View.OnClickListener() {            
            @Override public void 
            onClick(View v)
            {
                Context ctx = v.getContext();
                int nMinIdx = m_sb.getSelectedMinValue();
                int nMaxIdx = m_sb.getSelectedMaxValue();
                Pref.SetMinCameraPixelsIdx(ctx, nMinIdx);
                Pref.SetMaxCameraPixelsIdx(ctx, nMaxIdx);
                
                BaseActivity activity = (BaseActivity)getActivity();
                if (activity != null) {
                    Intent it = new Intent();
                    it.putExtra(EXTRA_CAMERA_PIXELS_MIN, _GetCameraPixels(nMinIdx, true));
                    it.putExtra(EXTRA_CAMERA_PIXELS_MAX, _GetCameraPixels(nMaxIdx, false));
                    
                    activity.notifyByFragment(FindDevicesByCameraFormFrg.this, it);
                }
            }
        });
        
        _UpdateSeekBar();
        return vRoot;
    }
    
    private void
    _UpdateSeekBar()
    {
        int nMin = m_sb.getSelectedMinValue();
        int nMax = m_sb.getSelectedMaxValue();
        m_tvMin.setText(_GetCameraPixelsText(nMin));
        m_tvMax.setText(_GetCameraPixelsText(nMax));
    }
    
    private int
    _GetCameraPixels(int nIdx, boolean bForMin)
    {
        int n = _GetCameraPixels(nIdx);
        if (bForMin) {
            n -= (500 * 1024);
            if (n < 0) n = 0;
        }
        else n += (500 * 1024);
        
        return n;
    }
    
    private int
    _GetCameraPixels(int nIdx)
    {
        if (nIdx == 0) return 100*1024;
        else return nIdx * 1024 * 1024;
    }
    
    private String
    _GetCameraPixelsText(int nIdx)
    {
        double dxPixels = _GetCameraPixels(nIdx) / (double)InfoViewCONSTS.M_LV;
        return new DecimalFormat("#.##").format(dxPixels) + 
                            getActivity().getString(R.string.di_ca_m_pixel);        
    }
}
